package com.tjetc.dao.impl;

import com.tjetc.dao.ProfitDao;
import com.tjetc.domain.Profit;
import com.tjetc.utils.Db;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class ProfitDaoImpl implements ProfitDao {
    @Override
    public List<Profit> list() throws SQLException {
        return Db.runner0().query("select * from profit ",new BeanListHandler<>(Profit.class));
    }

    @Override
    public List<Profit> listbydate(String startDate, String endDate) throws SQLException {
        return Db.runner0().query("SELECT * FROM profit WHERE date BETWEEN ? AND ?",new BeanListHandler<>(Profit.class),
                startDate,endDate);
    }

    @Override
    public Profit findByDate(String date) throws SQLException {
        return Db.runner0().query("select * from profit where date = ?",new BeanHandler<>(Profit.class),date);
    }

    @Override
    public void addProfit(Profit profit) throws SQLException {
        Db.runner0().update("insert into profit(total, date) VALUES (?,?)",profit.getTotal(),profit.getDate());
    }

    @Override
    public void updateProfit(Profit profit) throws SQLException {
        Db.runner0().update("update profit set total = ? where id = ?",profit.getTotal(),profit.getId());
    }
}
